package com.pureut.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.order.domain.SysCustomType;
import com.pureut.order.dto.SysCustomDto;
import com.pureut.order.mapper.SysCustomMapper;
import com.pureut.order.service.ISysCustomService;
import com.pureut.order.vo.SysCustomVo;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.core.utils.BeanCopyUtils;

import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 客户类别实现层
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ISysCustomServiceImp implements ISysCustomService {
    private final SysCustomMapper customMapper;

    /**
     * 查询客户类别数据
     *
     * @param customvo 客户类别
     * @return 客户类别集合
     */
    public TableDataInfo<SysCustomDto> selectCustomList(SysCustomVo customvo, PageQuery pageQuery) {
        Page<SysCustomDto> page = customMapper.selectSysCustom(pageQuery.build(), customvo);
        return TableDataInfo.build(page);

    }

    /**
     * 新增客户类别数据
     *
     * @param customvo 客户类别
     * @return
     */
    public int addSysCustom(SysCustomVo customvo) {
        //判断该编码是否存在
        SysCustomType sysCustomType1 = customMapper.selectOne(new QueryWrapper<SysCustomType>().lambda()
            .eq(SysCustomType::getTypeCode, customvo.getTypeCode()));
        if (sysCustomType1 != null) {
            throw new GlobalException("该编码已存在");
        }
        SysCustomType sysCustomType = new SysCustomType();
        BeanUtils.copyProperties(customvo, sysCustomType);
        sysCustomType.setCreateTime(new Date());
        return customMapper.insert(sysCustomType);
    }

    /**
     * 修改客户类别数据
     *
     * @param customvo 客户类别
     * @return
     */
    public int editSysCustom(SysCustomVo customvo) {
        SysCustomType sysCustomType = new SysCustomType();
        BeanUtils.copyProperties(customvo, sysCustomType);
        return customMapper.updateById(sysCustomType);
    }

    /**
     * 查询单个客户类别数据
     *
     * @param customId 客户类别id
     * @return
     */
    @Override
    public SysCustomDto viewSysCustom(Long customId) {
        SysCustomType sysCustomType1 = customMapper.selectOne(new QueryWrapper<SysCustomType>()
            .lambda()
            .eq(SysCustomType::getCustomId, customId));
        if (sysCustomType1 == null) {
            throw new GlobalException("未查到该数据！");
        }
        customMapper.selectOne(new QueryWrapper<SysCustomType>()
            .lambda()
            .eq(SysCustomType::getCustomId, customId));
        SysCustomDto sysCustomDto = new SysCustomDto();
        BeanUtils.copyProperties(sysCustomType1, sysCustomDto);
        return sysCustomDto;
    }

    /**
     * 删除客户类别数据
     *
     * @param id 客户类别id
     * @return
     */
    @Override
    public int removeSysCustom(Long[] id) {
        List<Long> ids = Arrays.asList(id);
        return customMapper.deleteBatchIds(ids);
    }

    /**
     * 客户类别下拉列表
     */
    @Override
    public List<HashMap<String, String>> downList() {
        List<HashMap<String, String>> resultMap = new ArrayList<>();
        List<SysCustomType> sysCustomTypes = customMapper.selectList();
        for (SysCustomType entity : sysCustomTypes) {
            HashMap<String, String> map = new HashMap<>();
            map.put("label", entity.getTypeName());
            map.put("value", entity.getTypeCode());
            resultMap.add(map);
        }
        return resultMap;
    }


}
